package com.modificial.swagger;

import com.google.common.base.Optional;
import io.swagger.models.Swagger;
import springfox.documentation.service.Documentation;
import springfox.documentation.spring.web.DocumentationCache;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.mappers.ServiceModelToSwagger2Mapper;

/**
 * @author modificial
 * @date 2018/7/31
 * @company codingApi
 * @description swagger2默认实现
 */
public class Swagger2Facade {

    private DocumentationCache documentationCache;

    private ServiceModelToSwagger2Mapper mapper;



    public Swagger2Facade(DocumentationCache documentationCache, ServiceModelToSwagger2Mapper mapper) {
        super();
        this.documentationCache = documentationCache;
        this.mapper = mapper;
    }



    public Swagger getDocumentation(String swaggerGroup) {
        String groupName = Optional.fromNullable(swaggerGroup).or(Docket.DEFAULT_GROUP_NAME);
        Documentation documentation = documentationCache.documentationByGroup(groupName);
        if (documentation == null) {
            return null;
        }
        return mapper.mapDocumentation(documentation);
    }
}
